package com.gua.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gua.common.enums.DeletedEnum;
import com.gua.common.enums.EnableEnum;
import com.gua.common.enums.HideEnum;
import com.gua.user.entity.DesktopMenu;
import com.gua.user.mapper.DesktopMenuMapper;
import com.gua.user.response.DesktopMenuDTO;
import com.gua.user.service.IDesktopMenuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class DesktopMenuImpl extends ServiceImpl<DesktopMenuMapper, DesktopMenu> implements IDesktopMenuService {

    @Override
    public List<DesktopMenuDTO> getDesktopMenu() {
        LambdaQueryWrapper<DesktopMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DesktopMenu::getStatus, EnableEnum.ENABLE.getCode());
        queryWrapper.eq(DesktopMenu::getDelFlag, DeletedEnum.NORMAL.getCode());
        //1.查出所有分类
        List<DesktopMenu> desktopMenus = baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(desktopMenus)) {
            desktopMenus = new ArrayList<>();
        }
        List<DesktopMenuDTO> desktopMenuDTOs = new ArrayList<>();
        for (DesktopMenu desktopMenu : desktopMenus) {
            DesktopMenuDTO desktopMenuDTO = new DesktopMenuDTO();
            desktopMenuDTO.setId(desktopMenu.getId());
            desktopMenuDTO.setPath(desktopMenu.getPath());
            desktopMenuDTO.setName(desktopMenu.getName());
            desktopMenuDTO.setLabel(desktopMenu.getLabel());
            desktopMenuDTO.setIcon(desktopMenu.getIcon());
            //true隐藏；false不隐藏
            desktopMenuDTO.setHide(desktopMenu.getHide() == HideEnum.HIDE.getCode() ? true : false);
            desktopMenuDTO.setUrl(desktopMenu.getUrl());
            desktopMenuDTO.setHideSliderBar(desktopMenu.getHideSliderBar() == HideEnum.HIDE.getCode() ? true : false);
            desktopMenuDTO.setParentId(desktopMenu.getParentId());
            desktopMenuDTO.setSort(desktopMenu.getSort());
            Map<String, Object> mata = new HashMap<>();
            mata.put("hideSliderBar", desktopMenuDTO.isHideSliderBar());
            desktopMenuDTO.setMeta(mata);
            desktopMenuDTOs.add(desktopMenuDTO);
        }
//        return desktopMenuDTOs;
        // https://blog.csdn.net/xie1016/article/details/123750710
        //2.组装成父子树形结构
        //2.1)、找到所有的一级分类
        List<DesktopMenuDTO> level1Menus = desktopMenuDTOs.stream().filter((desktopMenuDTO) -> {
            return desktopMenuDTO.getParentId() == 0;
        }).map((menu) -> {
            menu.setChildren(getChildren(menu, desktopMenuDTOs));
            return menu;
        }).sorted((menu1, menu2) -> {
            return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
        }).collect(Collectors.toList());
        return level1Menus;
    }

    /**
     * 递归查找所有菜单的子菜单
     * @param root
     * @param allDesktopMenuDTO
     * @return
     */
    private List<DesktopMenuDTO> getChildren(DesktopMenuDTO root, List<DesktopMenuDTO> allDesktopMenuDTO) {
        List<DesktopMenuDTO> children = allDesktopMenuDTO.stream()
                .filter((desktopMenuDTO) -> {
                    return desktopMenuDTO.getParentId() == root.getId();
                }).map((entity) -> {
                    //1.找到子菜单
                    entity.setChildren(getChildren(entity, allDesktopMenuDTO));
                    return entity;
                }).sorted((menu1, menu2) -> {
                    //2.菜单的排序
                    return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
                }).collect(Collectors.toList());
        return children;
    }

}
